
package managedbean.Product;

import entity.Product;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import session.stateless.ProductBeanLocal;


@ManagedBean(name="viewProductManagerBean")
@ViewScoped
public class ViewProductManagerBean implements Serializable{

    @EJB
    private ProductBeanLocal productBeanLocal;
    private Product selectedProduct;
    private String statusMessage;
    
    public ViewProductManagerBean() {
    }

    public Product getSelectedProduct() {
        return selectedProduct;
    }

    public void setSelectedProduct(Product selectedProduct) {
        this.selectedProduct = selectedProduct;
    }

    public String getStatusMessage() {
        return statusMessage;
    }

    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }
    
    public List<Product> getProductList(){
        List<Product> temp=productBeanLocal.getProductList();
        return productBeanLocal.getProductList();
    }
    
    public void editProduct(ActionEvent event) throws IOException{
        selectedProduct = this.getSelectedProduct();
        if (selectedProduct != null){
            FacesContext.getCurrentInstance().getExternalContext().getFlash().put("selectedProduct", selectedProduct);            
            FacesContext.getCurrentInstance().getExternalContext().redirect("editProduct.xhtml");                
        }
    }
    
    public void deleteProduct(ActionEvent event) throws IOException
    {
        selectedProduct = this.getSelectedProduct();
        try
        {
            productBeanLocal.deleteProduct(selectedProduct.getProductType()); 
            FacesContext.getCurrentInstance().getExternalContext().redirect("viewProduct.xhtml");
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
    } 
    
}
